package com.liuya.common.support.spring.data.redis;

import org.springframework.core.serializer.Serializer;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class MyDefaultSerializer implements Serializer<Object> {

    /**
     * Writes the source object to an output stream using Java serialization.
     * The source object must implement {@link Serializable}.
     * @see ObjectOutputStream#writeObject(Object)
     */
    @Override
    public void serialize(Object object, OutputStream outputStream) throws IOException {
//        if (!(object instanceof Serializable)) {
//            throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
//                    "but received an object of type [" + object.getClass().getName() + "]");
//        }
        try{
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(object);
            objectOutputStream.flush();
        } catch(Exception e) {
            e.printStackTrace();
           throw  new IOException(e.getMessage());
        }

    }

}